package modelo;

import hbt.AbstractHibernateManager;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.AperturaMesaEntity;


public class AperturaMesaDao extends AbstractHibernateManager {
	
	private static AperturaMesaDao instancia = null;
	
	public static AperturaMesaDao getInstancia() {
		if(instancia == null) {
			instancia = new AperturaMesaDao();
		}
		return instancia;
	}
	
	public boolean merge (AperturaMesaEntity ame){
		boolean result = false;
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.merge(ame);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[AperturaMesa MERGED ON TABLE aperturas_mesas]::OK");
			result = true;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return result;
	}
	
	public boolean save (AperturaMesaEntity ame){
		boolean result = false;
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.save(ame);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[AperturaMesa SAVED ON TABLE aperturas_mesas]::OK");
			result = true;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return result;
	}
	
	public boolean guardar (AperturaMesaEntity ame){
		boolean result = false;
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.persist(ame);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[AperturaMesa PERSISTED ON TABLE aperturas_mesas]::OK");
			result = true;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return result;
	}
	
	
	public boolean saveOrUpdate(AperturaMesaEntity ame){
		Session s= null;
		try {
			System.out.println("Estoy en el try\n");
			System.out.println("AperturaMesaDAO ID aperturaMesa"+ame.getId());
			s=getNewSession();
			s.beginTransaction();

			s.flush();
			System.out.println("pase el flush\n");
			s.saveOrUpdate(ame);
			System.out.println("pase el update\n");
			s.getTransaction().commit();
			System.out.println("[AperturaMesa (SAVED OR UPDATED) ON TABLE aperturaMesas]::OK");
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		} finally {
			closeSession(s);
		}
	}

	
	
	@SuppressWarnings("null")
	public List<AperturaMesaEntity> obtenerAperturasPorMozo(Integer legajo) {
		Session s = null;
		List<AperturaMesaEntity> aLista = null;		
		try {
					s=getNewSession();
					@SuppressWarnings("rawtypes")
					Iterator iLista = s.createQuery("FROM AperturaMesaEntity am "
								+ "JOIN am.mozo m"
								+ "WHERE m.legajo = :legajo")
								.setInteger("legajo", legajo).iterate();
					
					while(iLista.hasNext()) {
						AperturaMesaEntity entity = (AperturaMesaEntity) iLista.next();
						aLista.add(entity);
					}
				} catch (HibernateException e) {
					e.printStackTrace();
				} finally{
					closeSession(s);
				}
		return aLista;
	}
	
	@SuppressWarnings("null")
	public AperturaMesaEntity obtenerAperturasPorID(Integer id) {
		Session s = null;
		AperturaMesaEntity aperm = new AperturaMesaEntity();	
		try {
					s=getNewSession();					
				    aperm =(AperturaMesaEntity) s.createQuery("FROM AperturaMesaEntity am "
								+ "WHERE am.id =:id").setInteger("id", id).uniqueResult();					
					
				} catch (HibernateException e) {
					e.printStackTrace();
				} finally{
					closeSession(s);
				}
		return aperm;
	}
	
	public Integer obtenerUltimoIdAperturaMesa() {
		Session s = null;
		Integer ultimoId = 0;
			try {
				s=getNewSession();
				ultimoId = (Integer) s.createQuery("SELECT MAX(am.id) FROM AperturaMesaEntity am ").uniqueResult();
			} catch (HibernateException e) {
				e.printStackTrace();
			} finally {
				closeSession(s);
				}
		return ultimoId;
	}
	
}
